class Main extends eui.UILayer {


    protected createChildren(): void {
        super.createChildren();

        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin
        })

        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }

        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }

        //inject the custom material parser
        //注入自定义的素材解析器
        let assetAdapter = new AssetAdapter();
        egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());

        //初始化游戏
        GlobalManager.instance().init(this.stage);

        this.runGame().catch(e => {
            LogUtil.log(e);
        })
    }

    private async runGame() {
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        
        await this.loadResource();
        // const result = await RES.getResAsync("description_json")
        // this.startAnimation(result);
        // await platform.login();
        this.createGameScene();
        // const userInfo = await platform.getUserInfo();
        // LogUtil.log(userInfo);

        // await platform.showShareMenu();
    }

    private async loadResource() {
        try {
            let platform = egret.Capabilities.os;
            // if(platform == 'Windows PC')	
            // {
            //     await RES.loadConfig("/resource/default.res.json", "/resource");
            // }
            // else
            // {
            //     await RES.loadConfig("default.res.json", GameConst.resourceURL);
            // }
            await RES.loadConfig("resource/local.res.json", "/resource");
            await RES.loadGroup("preload", 1);
 
            await this.loadTheme();
            UIManager.instance().openUILogic(LoadingUI);
            await RES.loadGroup("necessary", 0);
            await RES.loadGroup("json", 0);
            // await RES.loadGroup('sound', 0);

            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.errorLog, this);
        }
        catch (e) {
            LogUtil.error(e);
        }
    }

    private errorLog(e)
    {
        LogUtil.error(e);
    }

    private onResourceProgress(event)
    {
        let logic = UIManager.instance().getLogicByName('LoadingUI') as LoadingUI;
        if(logic)
        {
            logic.onProgress(event.itemsLoaded, event.itemsTotal, event.groupName)
        }
    }

    private loadTheme() {
        return new Promise((resolve, reject) => {
            // load skin theme configuration file, you can manually modify the file. And replace the default skin.
            //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
            let theme = new eui.Theme("resource/default.thm.json", this.stage);
            theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                resolve();
            }, this);

        })
    }

    /**
     * 创建场景界面
     * Create scene interface
     */
    protected createGameScene(): void {
        LogUtil.log('资源加载完成...')
        DataJson.init();
        WeChatgGameManager.instance().init();
         //用code请求openID后登录
        HttpManager.instance().init();

        let platform = egret.Capabilities.os;
        if(platform == 'Windows PC')	
        {
            HttpManager.instance().token = GameConst.mnq_token;
            LoginPrase.instance().Login();
        }
        else
        {
            let self = this;
            let fail_call = function()
            {
                WeChatgGameManager.instance().Login();
            }
            WeChatgGameManager.instance().getStorage('fruit_token',()=>{
                let success_call = function(value)
                {
                    // HttpManager.instance().token = value;
                    LoginPrase.instance().Login();
                }
                WeChatgGameManager.instance().checkSession(success_call, self);
            },fail_call,this)
        }
    }
}
